﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.display.Loader;
	import com.LibraryMovie;
	import br.com.stimuli.loading.BulkLoader;
	import fl.motion.easing.*;
	import com.gskinner.motion.GTween;
	
	public class SimpleLoad extends MovieClip {

		private var loader_clp:MovieClip;
		private var bulkLoader : BulkLoader = new BulkLoader("bulk");
		private var moviePath:String = "entrance.swf"
		
		public function SimpleLoad () {
			
			
			// constructor code
			
			if (this.loaderInfo.parameters.p != null) {
				this.moviePath = this.loaderInfo.parameters.p
			}
			
			Load(this.moviePath);
		}
		
		private function Load(moviePath) {
			
			/* add items to be loaded */
			
			bulkLoader.add(moviePath);
			
			
			/* add event listeners for the loader itself :
			   event fired when all items have been loaded */
			   
			bulkLoader.addEventListener(BulkLoader.COMPLETE, onLoadMovieComplete);
			
			
			/* Setup Loader */
			
			loader_clp = LibraryMovie.attachMovieFromLibrary("LoaderClpTwo");			
			addChild(loader_clp);			
			loader_clp.x = stage.stageWidth / 2;
			loader_clp.y = stage.stageHeight / 2;
			
			/* start loading all items */
			
			bulkLoader.start();
			
			/* onMovieEnterFrame */
			
			this.addEventListener(Event.ENTER_FRAME, onMovieEnterFrame);
		}
		
		
		private function onMovieEnterFrame(e:Event):void {
			
			loader_clp.rotation += 10;
			
		}
		
		private function onLoadMovieComplete(evt : ProgressEvent){	
			
			
			/* Fade Out Loader */
			
			var tween1:GTween = new GTween (loader_clp, 1, { alpha:0 }, { ease:Exponential.easeOut } );
			tween1.addEventListener(Event.COMPLETE, myFunction);
				
			function myFunction(tween:Event):void {
					
				/* clear onMovieEnterFrame */
					
				//this.removeEventListener(Event.ENTER_FRAME, onMovieEnterFrame);
				removeChild(loader_clp);
				trace("tween completed");
			}
			
			/* grab the main movie clip: */
			
			var movieClp = bulkLoader.getMovieClip(this.moviePath);
			movieClp.x = 0;
			movieClp.y = 0;
			
			addChild(movieClp);			
			trace("Movie loaeded and ready to consume");
			
		}

	}
	
}
